<?php

/**
 * 资金账户
 */

namespace app\member\api;

use \app\member\api\MemberApi;

class FinanceApi extends MemberApi {

    /**
     * 财务记录
     */
    public function payLog() {
        $type = $this->data['type'];
        $page = intval($this->data['page']);
        $page = $page ? $page : 1;
        $pageLimit = 15;
        $model = target('member/PayLog');
        $where = array();
        $where['C.user_id'] = $this->data['auth_uid'];
        if($type == 'in') {
            $where['A.type'] = 1;
        }
        if($type == 'out') {
            $where['A.type'] = 0;
        }
        $count = $model->countList($where);
        $pageObj = new \dux\lib\Pagination($count, $page, $pageLimit);
        $pageData = $pageObj->build();
        $limit = [$pageData['offset'], $pageLimit];
        $list = $model->loadList($where, $limit);
        $data = array();
        $data['count_page'] = $pageData['page'];
        $data['cur_page'] = $pageData['current'];
        $data['data'] = $list;
        $this->success('ok', $data);
    }

    /**
     * 提现申请
     */
    public function cash() {

        if(!target('member/Finance', 'service')->cash($this->data['auth_uid'], $this->data['money'], $this->data['account'], $this->data['name'], $this->data['type'])) {
            $this->error(target('member/Finance', 'service')->getError());
        }
        $this->success('提现申请成功!');

    }

    /**
     * 提现记录
     */
    public function cashLog() {
        $type = $this->data['type'];
        $page = intval($this->data['page']);
        $page = $page ? $page : 1;
        $pageLimit = 15;
        $model = target('member/PayCash');
        $where = array();
        $where['C.user_id'] = $this->data['auth_uid'];
        if($type == 'pending') {
            $where['A.status'] = 0;
        }
        if($type == 'success') {
            $where['A.status'] = 1;
        }
        $count = $model->countList($where);
        $pageObj = new \dux\lib\Pagination($count, $page, $pageLimit);
        $pageData = $pageObj->build();
        $limit = [$pageData['offset'], $pageLimit];
        $list = $model->loadList($where, $limit);
        $data = array();
        $data['count_page'] = $pageData['page'];
        $data['cur_page'] = $pageData['current'];
        $data['data'] = $list;
        $this->success('ok', $data);
    }

}